<?php 

class Block extends Template {

	function __construct($file, $format){
		parent::__construct();
		$this->file = $this->read_file($file);
		$this->format = $format;
	}

}

class Template {

public $file;
public $recurse = false;
public $string;
public $format;
public $blocks = array();
protected $config = array();

	public function __construct(){
		global $baseDir, $tplDir;
		global $tplPrefix, $tplSuffix, $tplExtension;
		
		$this->config['dir'] = $baseDir . $tplDir . DIRECTORY_SEPARATOR;
		$this->config['ext'] = $tplExtension;
		$this->config['pfx'] = $tplPrefix;
		$this->config['sfx'] = $tplSuffix;
	}
	
	function block($name, $file, $format = array()){
		$this->blocks[] = $name;
		$this->$name = new Block($file,$format);
	}
	
	public function read_file($file){
	
		$tplfile = $this->config['dir'] . $file . $this->config['ext'];
		
		if(file_exists($tplfile) !== false){
			return file_get_contents($tplfile);
		} else if(is_string($file)){
			return $file;
		}
	}
	
	function __check_format($search){
		if(isset($this->format[$search])){
			return $this->format[$search];
		}
		return $search;
	}
	
	function recurse(){
		$this->recurse = true;
		return $this;
	}
	
	/**
	*	Simple Parse
	*	Parse numeric arrays
	**/
	function simple_parse($data, $index){
		foreach($data as $num => $value){
			if(!is_array($value)){
				$this->string .= $this->replace($index, $value);
			}
		}
		return $this->string;
	}
	
	function parse($data, $reverse = false){
	$string = $this->file;
	
	if(is_array($data) === false){
		return false;
	}
	
		foreach($data as $index => $value){
			if(is_array($value)){
				$this->string .= $this->parse($value);
			} else {
				$index = $this->__check_format($index);
				$string = $this->replace($index,$value,$string);
			}
		}
		
		return $string;
	}
	
	function replace($search,$replace,$file = null){
		if($file == null){
			$file = $this->file;
		}
		$parsed = str_replace("{@$search}", $replace, $file);
		return ($this->file != $parsed) ? $parsed : '' ;
	}
	
	public function display($file, $data = array()){
		$this->file = $this->read_file($file);
	
		foreach($this as $name => $value){
			if(is_object($value) && get_class($value) == "Block"){
				$this->string = $this->replace($name, $value->string, $this->string);
			}
		}
		
		foreach($data as $name => $value){
			if(!is_array($value)){
				if(@is_object($this->$name) === false){
					$this->string = $this->replace($name, $value, $this->string);
				}
			}
		}
		
		echo preg_replace("/{@+[a-zA-Z_]+}/i","",$this->string);
	}

}

class TB_Bldr extends Template {

	public $templates = array();

	function __construct($templates, $cellformat){
		$this->templates = $templates;
		
		$this->block('td', $templates['td'], $cellformat);
		$this->block('tr', $templates['tr']);
		$this->block('tb', $templates['tb']);
	}
	
	function build($data){
		$trstr = '';
		$this->tb->string ='';
		$this->tr->string ='';
		foreach($data as $row => $value){
			$this->td->string = '';
			$this->td->string = $this->td->parse($data[$row]);
			dbg($this->td);
			$this->tr->parse(array('row'=>$this->td->string));
		}
	
		$this->tb->parse(array('content'=>$this->tr->string));
		return $this->tb->string;
	}

}

?>